//
//  NewPhoneViewController.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/5/8.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "NewPhoneViewC.h"
#import "Util.h"
#import "UserModel.h"
#import <MBProgressHUD.h>
#import "PersonalViewC.h"

@interface NewPhoneViewC ()

@end

@implementation NewPhoneViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title =@"绑定新手机";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField == _newField) {
        NSString *name = _newField.text;
        [Util changeNSString:name];
        if (name.length != 11) {
            UIAlertView *alertvc = [[UIAlertView alloc] initWithTitle:@"提示"
                                                              message:@"请正确填写11位手机号码"
                                                             delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alertvc show];
            return ;
        }
        [[UserModel sharedInstance] checkAccountIsExist:name successTele:^(BOOL successed, unsigned int value) {
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            if (successed) {
                HUD.labelText = @"用户名可以使用";
                [HUD hide:YES afterDelay:1.5f];
            }
            
            else{
                if (value == 0) {
                    HUD.labelText = @"用户名已存在请重新填写";
                    [HUD hide:YES afterDelay:1.5f];
                }
                
                if (value == 2) {
                    HUD.labelText = @"程序出错";
                    [HUD hide:YES afterDelay:1.5f];
                }
            }
        }];
        
    }
    
    if (textField == _smsField) {
        NSString *code = _smsField.text;
        NSString *name = _newField.text;
        [Util changeNSString:code];
        [[UserModel sharedInstance] checkSmsCode:name smscode:code
                               successTeleNumber:^(BOOL successed, unsigned int value) {
                                   MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                                   if (successed) {
                                       HUD.labelText = @"验证码输入正确";
                                       [HUD hide:YES afterDelay:1.5f];
                                       [UserModel sharedInstance].currentUser.userPhone = name;
                                   }
                                   else{
                                       if (value == 1) {
                                           HUD.labelText = @"验证码输入错误请重新填写";
                                           [HUD hide:YES afterDelay:1.5f];
                                       }
                                   }
                               }];
    }
}

- (IBAction)saveBtn:(id)sender{
    
    [self dismissViewControllerAnimated:YES completion:^{
    }]; 
}

- (IBAction)smsBtn:(id)sender{
    
    NSString *tele = _newField.text;
    [Util changeNSString:tele];
    [[UserModel sharedInstance] getSmsCode:tele
     successTeleNum:^(BOOL successed, unsigned int value) {
        }];
    _smsBtn.enabled = NO;


}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
